var requestService = require('../../utils/requestService.js');
Page({
  data: {
    amounts: [
        {
        amount:10,
        type:'primary'
        },
        {
            amount:20,
            type:'default'
        },
        {
            amount:50,
            type:'default'
        },
        {
            amount:100,
            type:'default'
        }],
      diyAmount:{
          amount:'',
          type:'default',
          hidden:true
      },
      paycheckBt:{
          disabled: false,
          loading: false
      },
      rechargeAmount:10/**充值金额*/
  },
  onLoad: function () {
      // 设置本页导航标题
      wx.setNavigationBarTitle({
          title: '充值'
      })
    this.setData({

    })
  },
  /**金额选择*/
  selectAmount:function (event) {
      const amount=event.currentTarget.id;
      const amounts=this.data.amounts;
      amounts.forEach(function (item) {
          if(item.amount==amount){
              item.type="primary";
          }else{
              item.type="default";
          }
      });
      this.setData({
          amounts:amounts,
          "diyAmount.hidden":true,
          "diyAmount.amount":'',
          rechargeAmount:amount
      })
    },
    bindKeyInput:function(e){
        const inputValue=e.detail.value
        if(inputValue<=0 || inputValue>100){
            this.setData({
                "diyAmount.amount":'',
                rechargeAmount:''
            })
            wx.showModal({
                title: '提示',
                content: "充值金额应在1-100之间",
                showCancel: false
            })
        }else{
            this.setData({
                "diyAmount.amount":inputValue,
                rechargeAmount:inputValue
            })
        }
    },
    /**点击自定义金额按钮*/
    selectDiyAmount:function(){
        const amounts=this.data.amounts;
        amounts.forEach(function (item) {
            item.type="default";
        });
        this.setData({
            amounts:amounts,
            "diyAmount.hidden":false,
            rechargeAmount:''
        })
    },
    /**充值*/
    paycheckAction:function() {
        const that=this;
        const rechargeAmount=that.data.rechargeAmount;
        if(rechargeAmount!=''){
            that.setData({
                "paycheckBt.disabled": true,
                "paycheckBt.loading": true
            });
            requestService.sendPostRequest("/payment/wechat/recharge",{"amount":rechargeAmount},function (result) {
                const response=result.data;
                if(response.resCode==0){
                    const wechatPaySign=response.resData;
                    wx.requestPayment({
                        'timeStamp': wechatPaySign.timeStamp,
                        'nonceStr': wechatPaySign.nonceStr,
                        'package': wechatPaySign.prepay_package,
                        'signType': wechatPaySign.signType,
                        'paySign': wechatPaySign.paySign,
                        'success':function(res){
                            wx.redirectTo({
                                url: '../msg/msg_success?msg=余额充值成功&redirectUrl=../wallet/index&btText=返回我的钱包'
                            })
                        },
                        'fail':function(res){
                            that.setData({
                                "paycheckBt.disabled": false,
                                "paycheckBt.loading": false
                            });
                            console.log(res.errMsg);
                            wx.showModal({
                                title: '提示',
                                content: "支付失败",
                                showCancel: false
                            })
                        }
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: response.message,
                        showCancel: false
                    })
                    that.setData({
                        "paycheckBt.disabled": false,
                        "paycheckBt.loading": false
                    });
                }
            });
        }else{
            wx.showModal({
                title: '提示',
                content: "充值金额不正确",
                showCancel: false
            })
        }


    },
    /**充值协议*/
    agreementAction:function() {
        wx.navigateTo({
            url: '../paycheck/agreement'
        })
    }
})